/**
 *  Copyright (c) 2010 The edtf project.  All rights reserved.
 *
 *  This file is part of edtf (Event Driven Testing Framework).
 *
 *  edtf is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  edtf is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with edtf.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  This software consists of voluntary contributions made by many
 *  individuals.  For exact contribution history, see the revision
 *  history and logs, available at http://code.google.com/p/edtf.
 */
package org.edtf.engine.ws.jaxws;

import java.util.LinkedList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jws.WebMethod;
import javax.jws.WebService;
import org.xepf.utils.ObjectCreator;
import org.xepf.xml.adapter.NodeSerializer;
import org.xepf.xml.adapter.SimpleXPath;
import org.xepf.event.Event;
import org.xepf.event.EventManager;

/**
 *
 * 
 */
@WebService
public class MessageIO {

    EventManager eventManager;
    NodeSerializer s;

    public MessageIO() throws Exception {
        s = (NodeSerializer) ObjectCreator.get("NodeSerializer");
    }
    
    public void setEventManager( EventManager eventManager ) {
        this.eventManager = eventManager; 
    }

    @WebMethod
    public boolean write(String message) {
        
        // todo: as a future performance improvement, consider adding 
        // another method write(List<message>) for performing
        // the below XPath query only when more than one event should be posted
        
        try {
            // identify all the available events
            List<String> eventsXmlList = s.serializeAsList( SimpleXPath.query(message, "//xtemp:event") );
            
            // initialize the Event instances
            List<Event> eventsList = new LinkedList<Event>(); 
            for (String eventXml : eventsXmlList) {
                Event event;
                try
                { 
                    event = (Event) ObjectCreator.get("Event");
                    event.setSource("adapter");
                    event.initialize(eventXml);
                    if ( eventsList.add(event) == false ) {
                        return false;
                    }
                }
                catch(Exception ex)
                {
                    // an event was not processed correctly, lets log it and return
                    Logger.getLogger(MessageIO.class.getName()).log(Level.SEVERE,
                            "While processing event:\n" +  eventXml + "\n the following exception was encountered: " + ex );
                    return false;
                }
            }
            
            // place the Events on the event board
            eventManager.postEvents(eventsList);
            
        } catch (Exception ex) {
            Logger.getLogger(MessageIO.class.getName()).log(Level.SEVERE,
                    "While processing message:\n" +  message + "\n the following exception was encountered: " + ex);
            // cannot roll-back so just return..
            return false;
        }
        return true;
    }

    public boolean read(String message) {
        throw new UnsupportedOperationException("We only expect input messages.");
    }
}
